/**
 * @author SilverBiology, LLC
 * @website http://code.google.com/p/gbif-spreadsheet-processor/
*/
Ext.namespace('GBIF');
Ext.namespace('GBIF.GeoSpatial')

GBIF.GeoSpatial.QueryableLayerTree = function(config) {

	Ext.apply(this, config, {
			autoScroll: true
		,	animate: true
		,	enableDD: true
		,	border:false
		,	rootVisible:false
		,	cls: 'x-tree-noicon'
		,	root: {
					nodeType: 'async'
				,	text: 'Queryable Layers'
				,	draggable: false
				,	expanded:true
			}
	});

	GBIF.GeoSpatial.QueryableLayerTree .superclass.constructor.call(this, config);
			
}

Ext.extend(GBIF.GeoSpatial.QueryableLayerTree , Ext.tree.TreePanel, {	
		
		addNewFolder:function(node){
			var	nodetype = (node.type == 'button') ? false : true;
			
			if(nodetype){
				node.select()
			}
			
			var treeeditor = new Ext.tree.TreeEditor(this, { }, {
					allowBlank: false
				,	blankText: 'A name is required'
				,	selectOnFocus: true
			});
			
			var root = this.getRootNode();
			if(this.getSelectionModel().selNode){
				root = this.getSelectionModel().selNode;
			}
			
			if(root.attributes.type=='gbif.clb'){
				GBIF.GeoSpatial.Notice.msg(this.txtError,this.txtGBIFNodeError);
				return;		
			}
			
			var node = root.appendChild(new Ext.tree.TreeNode({
					text:'New Folder'
				,	allowDrag:true
				,	leaf:false
				,	expanded:true
			}));
			 
			node.attributes.id =  node.text;
			 
			this.getSelectionModel().select(node);
			setTimeout(function(){
				treeeditor.editNode = node;
				treeeditor.startEdit(node.ui.textNode);
			}, 10);
		}
		
	,	editLayerName:function(node){
			var	nodetype = (node.type == 'button') ? false : true;
			if (nodetype) {
				node.select();
			} else {
				if (this.getSelectionModel().selNode) {
					node = this.getSelectionModel().selNode;
				} else {
					GBIF.GeoSpatial.Notice.msg(this.txtError, this.txtNodeError);
					return;
				}	
			}	
			
			if (node.isRoot) {
				GBIF.GeoSpatial.Notice.msg(this.txtError,this.txtRootNodeEditError);
				return;
			}
			
			var treeeditor = new Ext.tree.TreeEditor(this, {}, {
					allowBlank: false
				,	blankText: 'A name is required'
				,	selectOnFocus: true
			});

			setTimeout(function(){
				treeeditor.editNode = node;
				treeeditor.startEdit(node.ui.textNode);
			}, 10);
		}
	
	,	deleteLayerName:function(node){
			var	nodetype = (node.type == 'button') ? false : true;
			if(nodetype){
				node.select();
			} else {
				if (this.getSelectionModel().selNode) {
					node = this.getSelectionModel().selNode;
				} else {
					GBIF.GeoSpatial.Notice.msg(this.txtError, this.txtNodeError);
					return;
				}
			}

			if (node.isRoot) {
				GBIF.GeoSpatial.Notice.msg(this.txtError,this.txtRootNodeError);
			} else {
				Ext.MessageBox.confirm(this.txtConfirm,this.txtConfirmMsg1 + node.text + this.txtConfirmMsg2, this.deleteNode);
			}	
		}

	,	deleteNode: function(btn, text){
			if (btn == 'yes') {
				var lt = Ext.getCmp('layertree');
				var node = lt.getSelectionModel().selNode;
				node.destroy();
			}
		}	
});